#!/bin/bash
# 修改 hostname
read -p "输入该master的主机名 - 不要和其他的重名 ！！！！：如: master > " mastername
echo "该master的主机名：$mastername"
hostnamectl set-hostname $mastername
# 查看修改结果
hostnamectl status
# 设置 hostname 解析
echo "127.0.0.1   $(hostname)" >> /etc/hosts

# 必要环境安装
# 配置源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

# 必要工具安装
yum update -y && \
yum install -y net-tools telnet nload iftop wget lrzsz && \
yum install -y iotop && \
yum install -y vim sshpass 

# docker 安装
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine && \
yum install -y yum-utils device-mapper-persistent-data lvm2 && \
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum install -y docker-ce docker-ce-cli && \
yum install -y epel-release && \
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum install -y docker-compose

## kubectl 安装

# 1.使用包管理工具安装 必要的k8s管理工具
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

# 2. 设置各种权限相关的配置 
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 2.1 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 2.3 关闭虚拟交换内存
swapoff -a
# 直接配置中关闭，防止开机重启后kubelet重启失败
sed -i 's/\/dev\/mapper\/centos-swap/#\/dev\/mapper\/centos-swap/' /etc/fstab 
# 2.4 设置docker相关配置
# Set up the Docker daemon
touch /etc/docker/daemon.json
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF
#2.5 设置bridge
# 开启路由转发功能以及iptables的过滤策略。

cat <<EOF >  /etc/sysctl.d/k8s.conf
#开启iptables中ipv6的过滤规则
net.bridge.bridge-nf-call-ip6tables = 1
#开启ipv4的过滤规则
net.bridge.bridge-nf-call-iptables = 1
#开启服务器的路由转发功能
net.ipv4.ip_forward = 1
EOF
# 执行命令使修改生效。
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

# Create /etc/systemd/system/docker.service.d
sudo mkdir -p /etc/systemd/system/docker.service.d
# Restart Docker
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker

# 3. 初始化control-plane
# 提前下载镜像
docker pull coredns/coredns:1.8.4

docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4

docker rmi coredns/coredns:1.8.4

#kubeadm config images pull --kubernetes-version v1.22.0  --image-repository registry.aliyuncs.com/google_containers
# 读取输入
read -p "输入该主机名的ip > " ipaddr
echo "该主机的ip地址：$ipaddr"
read -p "确认:Please input (Y/N) : " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then 
    echo "Yes!"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then 
    echo "No!"
    exit 0
else
    echo "Input Error"
    exit 0
fi

kubeadm init --kubernetes-version v1.22.1  --image-repository registry.aliyuncs.com/google_containers  --apiserver-advertise-address=$ipaddr --pod-network-cidr=10.244.0.0/16


kubeadm init --kubernetes-version v1.22.1  --image-repository registry.aliyuncs.com/google_containers  --apiserver-advertise-address=10.19.49.93 --pod-network-cidr=10.244.0.0/16

# 主节点配置（安装脚本推荐的）
sudo cp /etc/kubernetes/admin.conf $HOME
sudo chown $(id -u):$(id -g) $HOME/admin.conf
export KUBECONFIG=$HOME/admin.conf
mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config

# 3.1 安装flannel网络
kubectl apply -f https://gitee.com/Gao06/hello-world/raw/master/depoy/kube-flannel.yml

# 4. 安装dashboard的web管理界面
kubectl apply -f https://gitee.com/Gao06/hello-world/raw/master/depoy/recommended.yaml


# 5.1 创建ServiceAccount 的admin-sa.yml
# cat <<EOF > ./admin-sa.yaml
# kind: ClusterRoleBinding
# apiVersion: rbac.authorization.k8s.io/v1
# metadata:
#   name: admin
#   annotations:
#     rbac.authorization.kubernetes.io/autoupdate: "true"
# roleRef:
#   kind: ClusterRole
#   name: cluster-admin
#   apiGroup: rbac.authorization.k8s.io
# subjects:
# - kind: ServiceAccount
#   name: admin
#   namespace: kube-system
# ---
# apiVersion: v1
# kind: ServiceAccount
# metadata:
#   name: admin
#   namespace: kube-system
#   labels:
#     kubernetes.io/cluster-service: "true"
#     addonmanager.kubernetes.io/mode: Reconcile
# EOF
# kubectl create -f admin-sa.yaml
# ps: 删除用户相关信息,kubectl delete -f admin-sa.yaml

# 5.2 查看token
kubectl describe secret/$(kubectl get secret -n kube-system |grep admin|awk '{print $1}') -n kube-system

# 6.1 获取worker节点的join token
#kubeadm token create --print-join-command